﻿using System;
using System.Drawing;
using System.Windows.Forms;
//
using System.Runtime.InteropServices;

// SkypeScreenSaver, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace SkypeScreenSaver
{
    public partial class FormPreview : Form
    {
        //
        [DllImport("user32.dll")]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect);

        [DllImport("user32.DLL", EntryPoint = "IsWindowVisible")]
        private static extern bool IsWindowVisible(IntPtr hWnd);

        public static IntPtr previewHwnd = new IntPtr(0);
        public static bool previewMode = false;
        //
        public FormPreview()
        {
            InitializeComponent();
            //
            this.BackColor = Color.Black;//.DarkSeaGreen;
            this.ForeColor = Color.Black;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            //
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.TopMost = true;
            //
            SetParent(this.Handle, previewHwnd);

            // wklejamy okno w okno rodzica
            Rectangle ParentRect;
            GetClientRect(previewHwnd, out ParentRect);
            this.Size = ParentRect.Size;
            this.Location = new Point(0, 0);

            Cursor.Hide();
            //
            // timer służy do zakończenia programu, gdy rodzic się zakończył
            //
            timer1.Interval = 200;
            //
            timer1.Enabled = true;
            timer1.Start();
            //
            //
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (IsWindowVisible(previewHwnd) == false)
            {
                timer1.Enabled = false;
                //MyDebug.UdpPrint.Send("!IsWindowVisible(previewHwnd)");
                Application.Exit();
            }
        }
    }
}
